package com.kaiyidiannao;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.kaiyidiannao.common.util.SharedPreferencesUtil;
import com.kaiyidiannao.data.DataCenter;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by 大志 on 14-4-10.
 */
public class KaiyiApplicaton extends Application {

    private static KaiyiApplicaton mInstance = null;

    public boolean m_bKeyRight = true;
    public BMapManager mBMapManager = null;

    public static final String strKey = "ObGaI7xKul69oR45W3X3YkGv";

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        //初始化DataCenter
        DataCenter.getInstance().initialize(getApplicationContext());
        //初始化Imageloadeer
        this.initImageLoader(getApplicationContext());
        //初始化百度地图
        this.initEngineManager(getApplicationContext());
        SharedPreferencesUtil.init(getApplicationContext());
    }

    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs() // Remove for release app
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    public void initEngineManager(Context context) {
        if (mBMapManager == null) {
            mBMapManager = new BMapManager(context);
        }

        if (!mBMapManager.init(strKey,new MyGeneralListener())) {
            Toast.makeText(KaiyiApplicaton.getInstance().getApplicationContext(),
                    "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
        }
    }

    public static KaiyiApplicaton getInstance() {
        return mInstance;
    }


    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    static class MyGeneralListener implements MKGeneralListener {

        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(KaiyiApplicaton.getInstance().getApplicationContext(), "您的网络出错啦！",
                        Toast.LENGTH_LONG).show();
            }
            else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(KaiyiApplicaton.getInstance().getApplicationContext(), "输入正确的检索条件！",
                        Toast.LENGTH_LONG).show();
            }
            // ...
        }

        @Override
        public void onGetPermissionState(int iError) {
            //非零值表示key验证未通过
            if (iError != 0) {
                //授权Key错误 ：
//                Toast.makeText(KaiyiApplicaton.getInstance().getApplicationContext(),
//                        "请在 BaiduMapApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常！error: " + iError, Toast.LENGTH_LONG).show();
                KaiyiApplicaton.getInstance().m_bKeyRight = false;
            }
            else{
                KaiyiApplicaton.getInstance().m_bKeyRight = true;
//                Toast.makeText(KaiyiApplicaton.getInstance().getApplicationContext(), "key认证成功", Toast.LENGTH_LONG).show();
            }
        }
    }


}
